package com.codeyang.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;

import java.security.KeyPair;

/**
 * 描述: TO-删除标志-DO
 * 授权的配置
 *
 * @author CodeYang_Site
 * @version 2021/5/22 22:10
 */
@Configuration
public class AuthorizationConfig extends AuthorizationServerConfigurerAdapter {

    /**
     * AuthenticationManager 身份认证管理器
     */
    @Autowired
    private AuthenticationManager authenticationManager;




//    /**
//     * 直接拉取bean
//     */
//    @Autowired
//    private JwtAccessTokenConverter jwtAccessTokenConverter;
//
//    public TokenStore tokenStore() {
//        return new JwtTokenStore(jwtAccessTokenConverter);
//    }

    /**
     * 使用jwt存放token
     * @return
     */
    //    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    /**
     * jwt转换器
     * 使用非对称加密
     *
     * @return
     */
//    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        // 非对称加密
        ClassPathResource resource = new ClassPathResource("codeyang-jwt.jks");
        // 创建一个钥匙工厂
        KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(resource, "codeyang123".toCharArray());
        // 创建一个钥匙
        KeyPair keyPair = keyStoreKeyFactory.getKeyPair("codeyang-jwt");
        // 设置钥匙
        jwtAccessTokenConverter.setKeyPair(keyPair);
        return jwtAccessTokenConverter;
    }



    /**
     * 配置第三方应用
     * 内存中
     * 也可以在jdbc中
     * <p>
     * 配置授权模式
     * password  密码认证
     * client_credentials 客户端认证
     *
     * @param clients
     * @throws Exception
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("web")
                .secret(passwordEncoder().encode("web-secret"))
                .authorizedGrantTypes("password") // 密码授权 就是普通的登录
                .accessTokenValiditySeconds(14400)
                .scopes("all")
                .redirectUris("https://www.baidu.com")
                .and()
                .withClient("client")
                .secret(passwordEncoder().encode("client-secret"))
                .authorizedGrantTypes("client_credentials") // 客户端授权  给一个应用颁发一个token
                .scopes("all")
                .redirectUris("https://www.baidu.com")
                // TODO 设置手动申请 第三方token fegin 远程调用传递token 申请的过期时间
                .accessTokenValiditySeconds(Integer.MAX_VALUE); // token过期时间
    }


    /**
     * 暴露服务端点
     *
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
       endpoints.tokenStore(tokenStore())
               .accessTokenConverter(jwtAccessTokenConverter())
               .authenticationManager(authenticationManager);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
